package com.ruoyi.web.controller.greatWall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.TreeData;
import com.ruoyi.system.domain.greatWall.Years;
import com.ruoyi.system.service.YearsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description: 文物
 * @Author: quavario
 * @Date:   2019-05-23
 * @Version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/years")
public class YearsController extends BaseController {
	@Autowired
	private YearsService yearsService;

    /**
     * 分页列表查询
     */
    @GetMapping
    public TableDataInfo list(Years years) {
    	startPage();
		QueryWrapper<Years> wrapper=new QueryWrapper<>();
		wrapper.like("name",years.getName());
		return getDataTable(yearsService.list(wrapper));
	}


	/**
	  *   添加/编辑
	 * @param years
	 * @return
	 */
	@PutMapping
    public Years save(@RequestBody Years years) {
		yearsService.saveOrUpdate(years);
		return years;
    }

	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
		Years years = yearsService.getById(id);
		yearsService.removeById(id);
    }

	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable String id) {
		Years years = yearsService.getById(id);
		return success(years);
    }

	@GetMapping("/tree")
	public TableDataInfo tree() {
		List<Years> list = yearsService.list();
		List<Years> years = TreeData.toTreeList(list).stream().sorted(Comparator.comparing(Years::getSort)).collect(Collectors.toList());
		return getDataTable(years);
	}

	@GetMapping("/list")
	public List<Years> list() {
		List<Years> list = yearsService.list(
				Wrappers.<Years>lambdaQuery().eq(Years::getType, 2)
		);
		return list;
	}

}
